﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;

namespace hdbankwebservice.DAO
{
    public class UserDAO
    {
        public static Users Login(string userName, string password)
        {
            SQLDataAccessHelper dl = new SQLDataAccessHelper();
            string sqlString = @"SELECT *
                                FROM USERS
                                WHERE (USERNAME = @UserName) AND (PASSWORD = @Password)";
            List<DbParameter> listparams = new List<DbParameter>();

            DbParameter para = dl.DoiSo();
            para.ParameterName = "UserName";
            para.DbType = DbType.String;
            para.Direction = ParameterDirection.Input;
            para.Value = userName;
            listparams.Add(para);

            para = dl.DoiSo();
            para.ParameterName = "Password";
            para.DbType = DbType.String;
            para.Direction = ParameterDirection.Input;
            para.Value = password;
            listparams.Add(para);

            SqlCommand sql = new SqlCommand(sqlString);
            DataTable dt = dl.ThucThiTruyVan(sql, listparams);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                Users user = new Users();
                user.UserName = dt.Rows[0]["USERNAME"].ToString();
                user.PassWord = dt.Rows[0]["PASSWORD"].ToString();
                user.MaKhachHang = dt.Rows[0]["MAKHACHHANG"].ToString();
                user.MaNhomKhachHang = (int)dt.Rows[0]["MANHOMKHACHHANG"];
                return user;
            }
        }
        public static Users LayThongTinUser(string userName)
        {
            SQLDataAccessHelper dl = new SQLDataAccessHelper();
            string sqlString = @"SELECT *
                                FROM USERS
                                WHERE USERNAME = @UserName";
            List<DbParameter> listparams = new List<DbParameter>();

            DbParameter para = dl.DoiSo();
            para.ParameterName = "UserName";
            para.DbType = DbType.String;
            para.Direction = ParameterDirection.Input;
            para.Value = userName;
            listparams.Add(para);

            SqlCommand sql = new SqlCommand(sqlString);
            DataTable dt = dl.ThucThiTruyVan(sql, listparams);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                Users user = new Users();
                user.UserName = dt.Rows[0]["USERNAME"].ToString();
                user.PassWord = dt.Rows[0]["PASSWORD"].ToString();
                user.MaKhachHang = dt.Rows[0]["MAKHACHHANG"].ToString();
                user.MaNhomKhachHang = (int)dt.Rows[0]["MANHOMKHACHHANG"];
                return user;
            }
        }
    }
}